﻿using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace HlcLogViewer
{
    public partial class Form_Main : Form
    {
        public Form_Main()
        {
            InitializeComponent();
        }

        #region 事件

        private void Form_Main_Load(object sender, EventArgs e)
        {
            InitLogLevelClb();
        }

        /// <summary>
        /// Select按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_selectLogFiles_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.Multiselect = true;
            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            var fileNames = dialog.FileNames;

            ShowLogFiles(fileNames);
        }

        /// <summary>
        /// View按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_viewLogs_Click(object sender, EventArgs e)
        {
            ViewLogs();
        }

        /// <summary>
        /// Search按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_search_Click(object sender, EventArgs e)
        {
            var searchTxt = txt_search.Text.Trim().ToLower();
            bool visible = true;

            foreach (DataGridViewRow row in dgv_logView.Rows)
            {
                visible = false;
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.Value.ToString().ToLower().Contains(searchTxt))
                    {
                        visible = true;
                        break;
                    }
                }

                row.Visible = visible;
            }
        }

        /// <summary>
        /// Log单元格双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_logView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }

            var log = dgv_logView.Rows[e.RowIndex].Tag as LogModel;
            if (log == null)
            {
                return;
            }

            using (var logDetail = new Form_LogDetail(log))
            {
                logDetail.ShowDialog();
            }
        }

        /// <summary>
        /// 文件拖入响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_logView_DragDrop(object sender, DragEventArgs e)
        {
            var fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);

            ShowLogFiles(fileNames);
            ViewLogs();
        }


        private void dgv_logView_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 初始化Log级别下拉框
        /// </summary>
        private void InitLogLevelClb()
        {
            foreach (var enumName in typeof(LogLevel).GetEnumNames())
            {
                clb_logLevel.Items.Add(enumName, true);
            }
        }



        /// <summary>
        /// 显示Log文件名到Log文件名列表中
        /// </summary>
        /// <param name="fileNames">要显示的文件全路径</param>
        /// <param name="isClearOld">是否清楚原有的文件名</param>
        /// <param name="isChecked">是否默认选中</param>
        private void ShowLogFiles(string[] fileNames, bool isClearOld = true, bool isChecked = true)
        {
            if (isClearOld)
            {
                clb_logFiles.Items.Clear();
            }

            clb_logFiles.DisplayMember = "FileName";
            clb_logFiles.ValueMember = "FullPath";

            foreach (var fileName in fileNames)
            {
                clb_logFiles.Items.Add(new LogFile(fileName), isChecked);
            }
        }

        /// <summary>
        /// 在DataGridView中显示log
        /// </summary>
        private void ViewLogs()
        {
            dgv_logView.Rows.Clear();
            var selectedFiles = clb_logFiles.CheckedItems.Cast<LogFile>();
            foreach (var logFile in selectedFiles)
            {
                var logModels = LogModel.ParseLogFile(logFile.FullPath);
                logModels.ForEach(log =>
                {
                    if (!clb_logLevel.CheckedItems.Cast<string>().Contains(log.LogLevel.ToString()))
                    {
                        return;
                    }

                    var rowIndex = dgv_logView.Rows.Add();
                    var row = dgv_logView.Rows[rowIndex];

                    row.DefaultCellStyle.ForeColor = LogViewUtil.ConvertForColor(log.LogLevel, row.DefaultCellStyle.ForeColor);
                    row.DefaultCellStyle.BackColor = LogViewUtil.ConvertBackColor(log.LogLevel, row.DefaultCellStyle.BackColor);
                    row.Cells["DateTime"].Value = log.DateTime;
                    row.Cells["Level"].Value = log.LogLevel;
                    row.Cells["Thread"].Value = log.Thread;
                    row.Cells["Message"].Value = log.Message + (string.IsNullOrEmpty(log.Exception) ? "" : "\n" + log.Exception);
                    row.Tag = log;
                });
            }
        }

        #endregion

    }
}
